package com.banksoft.front.util;

import com.banksoft.admin.SystemCache;
import com.banksoft.admin.bean.WxSetting;
import com.banksoft.util.DateUtil;
import com.banksoft.util.StringUtil;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * Created by vanjoger on 2017/1/2.
 */
public class SendMessageUtil {

    public static  String sendMessage(String openId,String url,String first,Long time,String key1){
        try {
            String sendStr = "{\n" +
                    "\"touser\":\"" + openId + "\",\n" +
                    "\"template_id\":\"" + SystemCache.Template_Id + "\",\n" +
                    "\"topcolor\":\"#FF0000\",\n" +
                    "\"url\":\"" + url + "\",\n" +
                    "\"data\":{\n" +
                    "\"first\": {\n" +
                    "\"value\":\"" + first +"\",\n"+
                    "\"color\":\"#173177\"\n" +
                    "},\n" +
                    "\"keyword1\":{\n" +
                    "\"value\":\""+key1+"\",\n" +
                    "\"color\":\"#173177\"\n" +
                    "},\n" +
                    "\"keyword2\":{\n" +
                    "\"value\":\"" + DateUtil.getDateFromLong(time, "yyyy-MM-dd HH:mm:ss") + "\",\n" +
                    "\"color\":\"#173177\"\n" +
                    "},\n" +
                    "\"remark\":{\n" +
                    "\"value\":\"\"," +
                    "\"color\":\"#173177\"\n" +
                    "}\n" +
                    "}\n" +
                    "}";
            System.out.println("发送模板消息 ---------   " + sendStr);
            String json = WeixinUtil.templateMessage(sendStr);
            System.out.println("发送模板消息返回的消息 *******   " + json);
            String code = getJsonResult(json);
            if (code != null && code.equals("0"))
                return code;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 格式化json字符串   取返回结果
     * @param json
     * @return
     */
    public  static String getJsonResult(String json){
        if(StringUtil.isNotNull(json)){
            JSONObject obj = JSONObject.fromObject(json);
            return obj.get("errcode")+"";
        }
        return null;
    }





    public static void main(String[] args){
        String openId = "okgW1w09kAdygRAyFeJIW_--Y_7I";
      /*  String result = sendMessage(openId,null,"你好，有新的返利到账",new Date().getTime(),"感谢您的参与");
        System.out.println(result);*/
        try {
          Map map=  RedPacketUtil.sendRedPacket("12334addwdwdwdw", "荩美佳", openId, "10000", "提现资金到账",
                    "192.168.2.110", "资金到账啦！", "继续努力哦", SystemCache.getPaySetting());
            System.out.print("suc "+map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
